package com.jason.quectel.activities;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jason.quectel.R;
import com.jason.quectel.bean.User;
import com.jason.quectel.utils.MySQLiteOpenHelper;

import androidx.appcompat.app.AppCompatActivity;

public class SQLiteTestActivity extends AppCompatActivity {
    private TextView tvResult;
    private EditText etName;
    private EditText etAge;
    private SQLiteDatabase writableDatabase;
    private SQLiteDatabase readableDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite_test);
        tvResult = findViewById(R.id.tv_result);
        etName = findViewById(R.id.et_name);
        etAge = findViewById(R.id.et_age);
        String dbName = "quectel";
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this, dbName, null, 1);
        writableDatabase = helper.getWritableDatabase();
        readableDatabase = helper.getReadableDatabase();
    }

    public void insert(View view) {
        Editable name = etName.getText();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "请先输入名字", Toast.LENGTH_SHORT).show();
            return;
        }
        Editable age = etAge.getText();
        if (TextUtils.isEmpty(age)) {
            Toast.makeText(this, "请先输入年龄", Toast.LENGTH_SHORT).show();
            return;
        }
        writableDatabase.execSQL("insert into user (name,age) values(\"" + name.toString() + "\"," + age.toString() + ")");
        tvResult.setText("插入成功");
    }

    public void delete(View view) {
        Editable name = etName.getText();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "请先输入名字", Toast.LENGTH_SHORT).show();
            return;
        }
        writableDatabase.execSQL("delete from user where name=\"" + name.toString() + "\"");
        tvResult.setText("删除成功");
    }

    public void query(View view) {
        Editable name = etName.getText();
        Cursor cursor;
        if (TextUtils.isEmpty(name)) {
            cursor = readableDatabase.rawQuery("select * from user", null);
        } else {
            cursor = readableDatabase.rawQuery("select * from user where name=?", new String[]{name.toString()});
        }
        int count = cursor.getCount();
        StringBuilder sb = new StringBuilder();
        while (cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String nameStr = cursor.getString(1);
            int age = cursor.getInt(2);
            User user = new User(id, nameStr, age);
            sb.append(user).append("\n");
        }
        tvResult.setText("共查询到" + count + "条数据：\n" + sb);
        cursor.close();
    }

    public void update(View view) {
        Editable name = etName.getText();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "请先输入名字", Toast.LENGTH_SHORT).show();
            return;
        }
        Editable age = etAge.getText();
        if (TextUtils.isEmpty(age)) {
            Toast.makeText(this, "请先输入年龄", Toast.LENGTH_SHORT).show();
            return;
        }
        writableDatabase.execSQL(" update user set age=" + age.toString() + " where name=\"" + name.toString() + "\"");
        tvResult.setText("更新成功");
    }

    public void clear(View view) {
        etName.setText("");
        etAge.setText("");
    }
}